<template>
  <div class="post-comment">
    <van-field
      v-model.trim="message"
      rows="2"
      autosize
      type="textarea"
      maxlength="10"
      placeholder="请输入留言"
      show-word-limit
    >
    </van-field>
    <van-button size="mini" @click="onPost" :disabled="!message.length"
      >发布</van-button
    >
  </div>
</template>

<script>
import { addComment } from "@/api/comment";
export default {
  name: "PostComment",
  props: {
    target: {
      type: [Number, String, Object],
      required: true,
    },
    articleId: {
      type: [Number, String, Object],
      default: null,
    },
  },
  data() {
    return {
      message: "", // 请输入内容
    };
  },
  methods: {
    async onPost() {
      this.$toast.loading({
        message: "发布中...",
        forbidClick: true,
        duration: 0,
      });
      try {
        const { data } = await addComment({
          target: this.target,
          content: this.message,
          art_id: this.articleId ? this.articleId.toString() : null,
        });
        // 关闭弹出层
        // 将发布内容显示到列表顶部
        // 清除文本框
        this.message = "";
        this.$emit("post-success", data.data);
        this.$toast.success("发布成功");
      } catch (err) {
        this.$toast.fail("发布失败");
      }
    },
  },
};
</script>

<style scoped lang="less">
.post-comment {
  display: flex;
  align-items: center;
  font-size: 14px;
}
.van-field {
  flex: 1;
}
.van-button {
  margin-right: 10px;
}
</style>
